
<template>
  <basic-container>
    <div class="app-container calendar-list-container">
      <avue-form ref="form"
                 v-model="form"
                 :option="option"
                 @submit="submitJbxx"
                 :ltable-loading="loading">
        <template slot="groupHeader">
          <li class=" el-icon-info avue-group__icon "
              style=" display:flex;">
            <h1 class=" avue-group__title "
                style="padding-top:2px;padding-left:4px ">个人信息</h1>
          </li>
          <el-button @click="getIdCard"
                     style="width: 100px;text-align:center; margin-left:60%;padding:10px 0px"
                     type="primary">识别身份证</el-button>
        </template>
      </avue-form>
    </div>
  </basic-container>
</template>
<script>
import {
  addObj,
  updateObj,
  getObj,
  getIdCard,
  getObjByIdCard
} from '@/api/cust/base'
import { getValue } from '@/const/cust/cust_dic'
import { mapGetters } from 'vuex'
import option from '@/const/cust/jbxx'
import { getStore } from '@/util/store'
import { getAge } from '@/filters/'
export default {
  name: 'table_user',
  data () {
    return {
      loading: false,
      option: option(this),
      custId: '',
      form: {},
      org: {}
    }
  },
  watch: {
    'form.ywgms' () {
      this.colDisplay('ywgms', '5', 24, '药物过敏史')
    },
    'form.jzsfq' () {
      this.colDisplay('jzsfq', '12', 24, '家族史-父亲')
    },
    'form.jzsmq' () {
      this.colDisplay('jzsmq', '12', 24, '家族史-母亲')
    },
    'form.jzsxdjm' () {
      this.colDisplay('jzsxdjm', '12', 24, '家族史-兄弟姐妹')
    },
    'form.jzszn' () {
      this.colDisplay('jzszn', '12', 24, '家族史-子女')
    },
    'form.ycbs' () {
      this.colDisplay('ycb', '2', 24, '遗传病名称')
    },
    'form.cjqk' () {
      this.colDisplay('cjqk', '8', 24, '其他残疾')
    }
  },
  computed: {
    ...mapGetters(['permissions'])
  },
  created () {
    this.org = getStore({ name: 'org' })
  },
  mounted () {
    this.custId = this.$route.query.custId
    this.init()
  },
  methods: {
    getIdCard () {
      getIdCard()
        .then(res => {
          var result = res.data
          if (typeof res.data == 'object') {
            this.$message.error('身份证识别失败，请重试')
            return
          }
          var data = res.data == null ? '' : res.data.replace(/\\/g, '/')
          try {
            result = JSON.parse(data)
          } catch (e) {
            result = eval('(' + data + ')')
          }
          if (result.ret === 0) {
            this.form.name = result.Certificate.Name
            this.form.xb = result.Certificate.Sex === '男' ? '1' : '2'
            this.form.csrq = result.Certificate.Birthday
            this.form.sfzh = result.Certificate.IDNumber
            this.form.age = getAge(result.Certificate.IDNumber)
            this.form.minzu = getValue('MINZU', result.Certificate.Nation)
            this.getObjByIdCard(result.Certificate.IDNumber)
          } else {
            this.$message.error('身份证识别失败，请重试')
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    getObjByIdCard () {
      if (this.validatenull(this.form.sfzh) || this.form.sfzh.length != 18) {
        this.$message({
          showClose: true,
          message: '请输入正确的身份证号',
          type: 'error'
        })
        return false
      } else {
        this.loading = true
        getObjByIdCard({
          sfzh: this.form.sfzh,
          start: 0,
          length: 10
        }).then(res => {
          this.loading = false
          if (res.data.data != null) {
            this.custId = res.data.data.id
            this.$router.push({
              replace: true,
              path: '/cust/header/jbxx',
              query: {
                custName: res.data.data.name,
                custId: res.data.data.id
              }
            })
            this.getCust(res.data.data.id)
          }
        })
        //年龄
        this.form.age = getAge(this.form.sfzh)
        this.form.csrq = this.form.sfzh.substr(6, 8)
        console.log(parseInt() % 2)
        this.form.xb =
          parseInt(this.form.sfzh.substr(16, 1)) % 2 == 0 ? '2' : '1'
      }
    },
    init () {
      if (!this.validatenull(this.custId)) {
        this.getCust(this.custId)
      }
    },
    getCust (custId) {
      this.custId = custId
      getObj(custId).then(res => {
        this.form = res.data
        if (this.validatenull(this.form.minzu)) {
          this.form.minzu = '1'
        }
        if (this.validatenull(this.form.whcd)) {
          this.form.whcd = '99'
        }
        if (this.validatenull(this.form.ywgms)) {
          this.form.ywgms = '1'
        }
        if (this.validatenull(this.form.blshi)) {
          this.form.blshi = '1'
        }
        if (this.validatenull(this.form.jzsfq)) {
          this.form.jzsfq = '1'
        } if (this.validatenull(this.form.jzsmq)) {
          this.form.jzsmq = '1'
        } if (this.validatenull(this.form.jzsxdjm)) {
          this.form.jzsxdjm = '1'
        } if (this.validatenull(this.form.jzszn)) {
          this.form.jzszn = '1'
        } if (this.validatenull(this.form.ycbs)) {
          this.form.ycbs = '1'
        } if (this.validatenull(this.form.cjqk)) {
          this.form.cjqk = '1'
        } if (this.validatenull(this.form.czlx)) {
          this.form.czlx = '1'
        }
      })
    },
    colDisplay (key, value, span, message) {
      var flag = false
      const des = this.findColumnIndex(key + 'Des')
      if (key === 'ycb') {
        key = 'ycbs'
      }
      const index = this.findColumnIndex(key)

      const desCol = this.option.group[des[0]].column[des[1]]
      const column = this.option.group[index[0]].column[index[1]]
      const data = this.validatenull(this.form[key])
        ? this.form[key]
        : this.form[key].split(',')
      //data = data.length === 1 ? data.join('') : data
      flag = Array.isArray(data)
        ? data.includes(value)
        : this.form[key] === value

      if (flag) {
        desCol.display = true
        desCol.rules = [
          {
            required: true,
            message: '请输入' + message
          }
        ]
        column.span = span / 2
      } else {
        column.span = span
        desCol.display = false
        desCol.rules = []
      }
    },
    findColumnIndex (prop) {
      let formIndex = -1
      let groupIndex = 1
      this.option.group.forEach((ele, index) => {
        ele.column.forEach((cele, cindex) => {
          if (cele.prop === prop) {
            groupIndex = index
            formIndex = cindex
          }
        })
      })
      return [groupIndex, formIndex]
    },
    submitJbxx () {
      if (this.validatenull(this.custId)) {
        this.form.jgbh = this.validatenull(this.org) ? '' : this.org.jgbh
        addObj(this.form).then(res => {
          this.$message({
            showClose: true,
            message: '修改成功',
            type: 'success'
          })
          this.$router.push({
            path: '/cust/header/jbxx',
            query: {
              custName: res.data.name,
              custId: res.data.id
            }
          })
        })
      } else {
        updateObj(this.form).then(() => {
          this.$message({
            showClose: true,
            message: '修改成功',
            type: 'success'
          })
        })
      }
    }
  }
}
</script>